package com.delivery.deliverysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delivery.deliverysystem.exception.RException;
import com.delivery.deliverysystem.pojo.DAO.QuoteDTO;
import com.delivery.deliverysystem.pojo.DO.Quote;
import com.delivery.deliverysystem.pojo.VO.QuoteVO;
import com.delivery.deliverysystem.service.QuoteService;
import com.delivery.deliverysystem.dao.QuoteMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author Administrator
* @description 针对表【quote】的数据库操作Service实现
* @createDate 2025-05-22 22:48:42
*/
@Service
public class QuoteServiceImpl extends ServiceImpl<QuoteMapper, Quote>
    implements QuoteService{

    @Autowired
    private QuoteMapper quoteMapper;

    @Override
    public List<QuoteVO> getQuote(String type) {
        QueryWrapper<Quote> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(type)){
            queryWrapper.eq("type",type);
        }
        List<Quote> quotes = quoteMapper.selectList(queryWrapper);
        //如果type不为空，直接返回对应类型的报价
        if(!StringUtils.isBlank(type)){
            List<QuoteDTO> quoteProjects = new ArrayList<>();
            for(Quote quote:quotes){
                QuoteDTO quoteDTO = QuoteDTO.builder()
                        .quoteId(quote.getQuoteId())
                        .quoteName(quote.getQuoteName())
                        .name(quote.getName())
                        .unit(quote.getUnit())
                        .price(quote.getPrice())
                        .build();
                quoteProjects.add(quoteDTO);
            }
            QuoteVO quoteVO = QuoteVO.builder().quoteProjects(quoteProjects).type(type).image("").build();
            return Collections.singletonList(quoteVO);
        }
        //如果type为空，返回所有类型的报价
        List<QuoteVO> res = new ArrayList<>();
        Map<String,List<QuoteDTO>> map = new HashMap<>();
        for(Quote quote:quotes){
            String quoteType = quote.getType();
            QuoteDTO quoteDTO = QuoteDTO.builder()
                    .quoteId(quote.getQuoteId())
                    .quoteName(quote.getQuoteName())
                    .name(quote.getName())
                    .unit(quote.getUnit())
                    .price(quote.getPrice())
                    .build();
            map.computeIfAbsent(quoteType, k -> new ArrayList<>()).add(quoteDTO);
        }
        for(Map.Entry<String, List<QuoteDTO>> entry:map.entrySet()){
            QuoteVO quoteVO = QuoteVO.builder().quoteProjects(entry.getValue()).type(entry.getKey()).image("").build();
            res.add(quoteVO);
        }
        return res;
    }

    @Override
    public boolean updateQuote(List<QuoteDTO> quoteList) throws Exception{
            for(QuoteDTO quoteDTO:quoteList){
                Integer quoteId = quoteDTO.getQuoteId();
                if(quoteId==null) throw new RException("缺少单价id",500);
                Quote selectById = quoteMapper.selectById(quoteId);
                if(selectById==null) throw new RException("该单价不存在",500);
                Quote update = Quote.builder()
                        .quoteId(quoteId)
                        .quoteName(selectById.getQuoteName())
                        .name(quoteDTO.getName()!=null?quoteDTO.getName():selectById.getName())
                        .unit(quoteDTO.getUnit()!=null?quoteDTO.getUnit():selectById.getUnit())
                        .price(quoteDTO.getPrice()!=null?quoteDTO.getPrice():selectById.getPrice())
                        .type(selectById.getType())
                        .build();
                if(quoteMapper.updateById(update)<0) throw new RException("更新失败",500);
            }

        return true;
    }
}




